package com.atan.weblog.common.utils;

import com.atan.weblog.common.exception.BaseExceptionInterface;
import com.atan.weblog.common.exception.BizException;
import lombok.Data;

@Data
public class ResponseUtil<T> {
    //成功 失败
    private Boolean success = true;
    /**
     * 响应消息
     */
    private String message;

    /**
     * 响应码
     */
    private String code;

    /**
     * 响应数据
     */
    private T data;

    /**
     * 响应成功
     */
    public static <T> ResponseUtil<T> success() {
        return new ResponseUtil<>();
    }



    public static <T> ResponseUtil<T> success(T data) {
        ResponseUtil<T> tResponseUtil = new ResponseUtil<>();
        tResponseUtil.setData(data);
        return tResponseUtil;
    }


    /**
     * 响应失败
     */

    public static <T> ResponseUtil<T> fail() {
//        return new ResponseUtil<>();
        ResponseUtil<T> responseUtil = new ResponseUtil<>();
        responseUtil.setSuccess(false);
        return responseUtil;

    }

    public static <T> ResponseUtil<T> fail(String message) {
        ResponseUtil<T> tResponseUtil = new ResponseUtil<>();
        tResponseUtil.setSuccess(false);
        tResponseUtil.setMessage(message);
        return tResponseUtil;
    }

    public static <T> ResponseUtil<T> fail(String code, String message) {
        ResponseUtil<T> tResponseUtil = new ResponseUtil<>();
        tResponseUtil.setSuccess(false);
        tResponseUtil.setCode(code);
        tResponseUtil.setMessage(message);
        return tResponseUtil;
    }

    public static <T> ResponseUtil<T> fail(BizException bizException) {
        ResponseUtil<T> response = new ResponseUtil<>();
        response.setSuccess(false);
        response.setCode(bizException.getErrorCode());
        response.setMessage(bizException.getErrorMessage());
        return response;
    }

    public static <T> ResponseUtil<T> fail(BaseExceptionInterface baseExceptionInterface) {
        ResponseUtil<T> response = new ResponseUtil<>();
        response.setSuccess(false);
        response.setCode(baseExceptionInterface.getErrorCode());
        response.setMessage(baseExceptionInterface.getErrorMessage());
        return response;
    }
}
